<?php

import("data.db.DbAbstractTable");
import("data.db.DbFactory");
import("data.logic.user.User");

class DbUser extends DbAbstractTable {
	
	//
	// SQL Table description section
	//
	const TB_TABLENAME	= "logins";
	const TB_I			= 'lgn';
	
	const TB_LOGIN		= 'lgn_login';
	const TB_PASSWORD	= 'lgn_password';
	const TB_FIRSTNAME	= 'lgn_firstname';
	const TB_LASTNAME	= 'lgn_lastname';
	
	const TB_LASTVISIT	= 'lgn_lastvisit';
	const TB_CODE		= 'lgn_code';
	
	function _get_TABLENAME() {return self::TB_TABLENAME;}
	function _get_ID() {return self::TB_I."_id";}
	function _get_SUFFIX() {return self::TB_I;}
	//
	//
	
	var $login		= null;
	var $password	= null;
	
	var $firstname	= "";
	var $lastname	= "";
	
	var $code		= "";
	var $lastvisit	= "";
	
	function DbUser() {
		parent::DbAbstractTable();
		$this->code = md5(uniqid("newCode"));
	}
	
	function setLogin($login) {
		$this->login = $login;
	}
	function getLogin() {
		return $this->login;
	}
	
	function setPassword($password) {
		$this->password = md5($password);
	}
	function getPassword() {
		return $this->password;
	}
	
	function setFirstName($name) {
		$this->firstname = $name;
	}
	function getFirstName() {
		return $this->firstname;
	}
	function setLastName($name) {
		$this->lastname = $name;
	}
	function getLastName() {
		return $this->lastname;
	}
	
	function setCode($code) {
		$this->code = $code;
	}
	function getCode() {
		return $this->code;
	}
	
	function setLastVisit($date) {
		$this->lastvisit = $date;
	}
	function getLastVisit() {
		return $this->lastvisit;
	}
	
	// restore
	function fillEntity($arrResult) {
		parent::fillEntity($arrResult);
		$this->setLogin($arrResult[self::TB_LOGIN]);
		$this->setFirstName($arrResult[self::TB_FIRSTNAME]);
		$this->setLastName($arrResult[self::TB_LASTNAME]);
		//$this->setLastVisit($arrResult[self::TB_LASTVISIT]);
		$this->setCode($arrResult[self::TB_CODE]);
		
		//Forward Foreigns
		//$this->initForeign($arrResult, 'data.logic.catalog.Category');
	}
	
	//store
	function fillFields() {
		parent::fillFields();
		$this->fildsForStore[self::TB_LOGIN] = "'".$this->getLogin()."'";
		$this->fildsForStore[self::TB_PASSWORD] = "'".$this->getPassword()."'";
		$this->fildsForStore[self::TB_FIRSTNAME] = "'".$this->getFirstName()."'";
		$this->fildsForStore[self::TB_LASTNAME] = "'".$this->getLastName()."'";
		//$this->fildsForStore[self::TB_LASTVISIT ] = "'".$this->getLastVisit()."'";
		$this->fildsForStore[self::TB_CODE] = "'".$this->getCode()."'";
		
		//$this->fildsAddForeign('data.logic.catalog.Category');
	}
	
	// static find
	function findByCode($code) {
		return parent::__selectByCriteria(
			self::TB_CODE ."='".$code."'",
			'data.logic.user.User');
	}
	
	function findByLogin($login) {
		return parent::__selectByCriteria(
			self::TB_LOGIN."='".$login."'",
			'data.logic.user.User');
	}
	
	function findByEncLoginAndEncPassword($encLogin, $encPassword) {
		return parent::__selectByCriteria(
			self::TB_LOGIN."='".$encLogin."' and ".self::TB_PASSWORD."='".$encPassword."'",
			'data.logic.user.User');
	}
	
}

?>